﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using HolcimProject.Common;
using HolcimProject.Common.DTO;
using HolcimProject.Function;
namespace HolcimProject.Webpart.FindingFlow
{
    [ToolboxItemAttribute(false)]
    public class FindingFlow : WebPart
    {
        string state = "";
        int findingID = 0;
        #region Property

        public int FindingID
        {
            get
            {
                try
                {
                    return findingID;
                    //return Convert.ToInt32(Request.QueryString["findingid"]);
                    //return 10;
                }
                catch
                {
                    return 0;
                }
            }
        }

        public string CurrentState
        {
            get
            {
                try
                {
                    return state;
                    // return HttpContext.Current.Request.QueryString["state"];

                }
                catch
                {
                    return "";
                }
            }
        }


        public int FindingTaskID
        {
            get
            {
                try
                {
                    return Convert.ToInt32(HttpContext.Current.Request.QueryString["findingtaskid"]);
                    // return 6;
                }
                catch
                {
                    return 0;
                }
            }
        }

        public bool Flat
        {
            get
            {
                try
                {
                    return Convert.ToBoolean(HttpContext.Current.Request.QueryString["flat"]);
                    // return 6;
                }
                catch
                {
                    return false;
                }
            }
        }

        #endregion
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.       

        public FindingFlow()
        {
            this.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;

        }
        protected override void CreateChildControls()
        {
            bool isIL = false;
            bool isAuditor = false;
            if (FindingTaskID != 0)
            {
                FunctionFinding functionFinding = new FunctionFinding();
                ParameterDTO parameter = functionFinding.GetParameterByFindTaskID(FindingTaskID);
                if (parameter != null)
                {
                    state = parameter.State;
                    findingID = parameter.FindingID;
                }
                // check user had ISOAdmin Lead aditor
                
                if (FindingID != 0)
                {
                    // get current user
                    SPUser currentUser = Utilities.GetCurrentUser(Constants.Site_Url);

                    // Check user had lead auditor       
                    FindingDTO findingLoad = new FindingDTO();
                    findingLoad = functionFinding.GetFindingByID(FindingID, Constants.State_EditActionLog);
                    if (findingLoad != null)
                    {
                        if (currentUser.LoginName.ToUpper() == findingLoad.LeadAuditor.Split('#')[1].ToUpper())
                        {
                            isIL = true;
                        }

                        // check is auditor
                        if (currentUser.LoginName.ToUpper() == findingLoad.Auditor.Split('#')[1].ToUpper())
                        {
                            isAuditor = true;
                        }
                    }
                    //end
                    // check User had ISO admin
                    bool flat = Utilities.CheckUserExitsInGroup(Constants.Site_Url, Constants.Group_ISOAdmin, currentUser.LoginName);
                    if (flat == true)
                    {
                        isIL = true;
                    }
                    //end
                }
            }
            string _ascxPath = @"~/_controltemplates/HolcimProject/FindingFlow/FindingCreate.ascx";
            switch (CurrentState)
            {
                case "": _ascxPath = @"~/_controltemplates/HolcimProject/FindingFlow/FindingCreate.ascx";
                    break;
                case Constants.State_EditActionLog:
                    // if user is ISOadmin or lead auditor and Flat=true
                    if (Flat == true && isIL == true)
                    {
                        _ascxPath = @"~/_controltemplates/HolcimProject/FindingFlow/FindingCreate.ascx";
                    }
                    else if (Flat == true && isAuditor == true)
                    {
                        _ascxPath = @"~/_controltemplates/HolcimProject/FindingFlow/FindingCreate.ascx";
                    }
                    else
                    {
                        _ascxPath = @"~/_controltemplates/HolcimProject/FindingFlow/FindingUpdateActionLog.ascx";
                    }
                    break;
                case Constants.State_ReviewActionLog: _ascxPath = @"~/_controltemplates/HolcimProject/FindingFlow/FindingReviewActionLog.ascx";
                    break;
                case Constants.State_ApprovalActionLog_Auditor:
                    if (Flat == true && isIL == true)
                    {
                        _ascxPath = @"~/_controltemplates/HolcimProject/FindingFlow/FindingCreate.ascx";
                    }
                    else
                    {
                        _ascxPath = @"~/_controltemplates/HolcimProject/FindingFlow/FindingReviewActionLog.ascx";
                    }
                    break;
                case Constants.State_ApprovalActionLog_LeadAuditor: _ascxPath = @"~/_controltemplates/HolcimProject/FindingFlow/FindingReviewActionLog.ascx";
                    break;
                case Constants.State_UpdateEvidence: _ascxPath = @"~/_controltemplates/HolcimProject/FindingFlow/FindingUpdateEvidence.ascx";
                    break;
                case Constants.State_ApproveEvidence_Auditor: _ascxPath = @"~/_controltemplates/HolcimProject/FindingFlow/FindingReviewEvindence.ascx";
                    break;
                case Constants.State_ApproveEvidence_ISOAdmin: _ascxPath = @"~/_controltemplates/HolcimProject/FindingFlow/FindingReviewEvindence.ascx";
                    break;
                case Constants.Status_Finish:
                case Constants.State_ApproveEvidence_ISOAdmin_Finish: _ascxPath = @"~/_controltemplates/HolcimProject/FindingFlow/FindingReviewEvindence.ascx";
                    break;
            }
            Control control = Page.LoadControl(_ascxPath);
            Controls.Add(control);
        }
    }
}
